var handleData = function (_val) {
    console.log('Handle datatime:', _val);
    var _vals = _val.split('T')
    console.log(_vals)

    var newDatatime = _vals[0] + ' ' + _vals[1];

    console.log(newDatatime, 'newDatatime')

    var date = new Date(newDatatime.replace(/-/g, '/')).getTime();

    console.log(date)

    delete  _vals

    return date
}

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/groupbuy/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true},
            {label: '团购名称', name: 'name', index: 'name', width: 80},
            {label: '商品ID', name: 'commodityid', index: 'commodityid', width: 80},
            {label: '库存', name: 'reserve', index: 'reserve', width: 80},
            {
                label: '原价', name: 'oldprice', index: 'oldprice', width: 80, formatter: function (value, options, row) {
                return value / 100
            }
            },
            {label: '开始时间', name: 'starttime', index: 'startTime', width: 80},
            {label: '结束时间', name: 'endtime', index: 'endTime', width: 80},
            {label: '人数限制', name: 'restrict', index: 'restrict', width: 80},
            {
                label: '是否开团', name: 'isopen', index: 'isopen', width: 80, formatter: function (value, options, row) {
                if (value === 0) {
                    return '<span class="label label-primary">未开团</span>';
                }
                if (value === 1) {
                    return '<span class="label label-success">已开团</span>';
                }
                if (value === 2) {
                    return '<span class="label label-warning">已结束</span>';
                }
            }
            },
            {label: '创建时间', name: 'createtime', index: 'createTime', width: 80},
            {label: '创建人', name: 'createuser', index: 'createUser', width: 80},
            {
                label: '最低价',
                name: 'minprice',
                index: 'minprice',
                width: 80,
                formatter: function (value, options, row) {
                    return value / 100
                }
            },
            {
                label: '当前价格',
                name: 'currentprice',
                index: 'currentprice',
                width: 80,
                formatter: function (value, options, row) {
                    return value / 100
                }
            },
            {label: '规则', name: 'rule', index: 'rule', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            name: null
        },
        showList: true,
        title: null,
        groupbuy: {},
        sels: []
    },
    created: function () {
        $.ajax({
            url: baseURL + 'sys/commodity/findAll',
            type: "GET",
            contentType: "application/json",
            success: function (result) {
                vm.sels = result.list
            },
            error: function (error) {
                alert('数据加载失败!');
            }
        })
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.groupbuy = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        fix: function (num, length) {
            return ('' + num).length < length ? ((new Array(length + 1)).join('0') + num).slice(-length) : '' + num;
        },
        saveOrUpdate: function (event) {
            var url = vm.groupbuy.id == null ? "sys/groupbuy/save" : "sys/groupbuy/update";
            vm.groupbuy.starttime = handleData(vm.groupbuy.starttimeOld)
            vm.groupbuy.endtime = handleData(vm.groupbuy.endtimeOld)
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.groupbuy),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/groupbuy/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/groupbuy/info/" + id, function (r) {
                var time1 = r.tbGroupbuy.starttime
                var time2 = r.tbGroupbuy.endtime
                vm.groupbuy = r.tbGroupbuy;
                vm.groupbuy.starttimeOld = time1.replace(" ", "T")
                vm.groupbuy.endtimeOld = time2.replace(" ", "T")


                console.info(vm.groupbuy)
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
        }
    }
});